﻿using Futaba.SBus.Channels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Futaba.SBus.SampleApplication.ChannelList
{
	class ViewModel : NotifyPropertyChangedBase
	{
		public ViewModel()
		{
			using (var stream = GetType().Assembly.GetManifestResourceStream(typeof(MainWindow).Namespace + ".example_model_configuration.xml"))
			{
				Channels = Configuration.ModelConfigurationFactory.DefaultInstance.Load(stream).Channels;
			}
		}

		private Channel[] _internalChannels;
		public Channel[] Channels
		{
			get { return _internalChannels; }
			set
			{
				if (Equals(_internalChannels, value))
					return;
				_internalChannels = value;
				RaisePropertyChanged(); //RaisePropertyChanged("Channels");
			}
		}

		ICollectionView _channelsView;
		public ICollectionView ChannelsView
		{
			get
			{
				if (_channelsView == null)
				{
					var s = System.Windows.Data.CollectionViewSource.GetDefaultView(Channels);
					using (s.DeferRefresh())
					{
						s.SortDescriptions.Add(new SortDescription("ChannelId", ListSortDirection.Ascending));
						//s.GroupDescriptions.Add(new PropertyGroupDescription("ChannelId"));
					}
					_channelsView = s;
				}
				return _channelsView;
			}
		}

	}
}
